﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.DataStore;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil;
using NTTDATA.BEENChina.ResultCertification.Util;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Tele
{
    [Serializable]
    [VerfityAttribute(Id = "VerfityT0101", Name = "存在しない電話検証", CategoryType = EnumCategoryType.电话, Model = true)]
    public class VerfityT0101:VerfityOneResult
    {

        private string _prefixTelLength;

        [Category("检查的电话前缀位数"), Description("设置要检查的电话前缀位数，按[，]分割。")]
        [Editor(typeof(PropertyGridRichText), typeof(System.Drawing.Design.UITypeEditor))]
        public String PrefixTelLength
        {
            get { return _prefixTelLength; }
            set { _prefixTelLength = value; }
        }

        private IEnumerable<int> PrefixTelLengthArray
        {
            get
            {
                
                var preTelLenArray=  _prefixTelLength.Split(',');
                if (preTelLenArray.Length >0)
                {
                  var telLenth =  preTelLenArray.Select(t =>
                                              {
                                                  int length = 0;
                                                  if (StrHelper.IsNumeric(t))
                                                  {
                                                      int.TryParse(t, out length);
                                                  }
                                                  return length;
                                              });

                    return telLenth;
                }

                return null;
            }
        }



        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {

            //默认电话不对
            //直到在DB中检索到
            bool notfind = false;

            string errorPrefixTel = string.Empty ,errTel = string.Empty ;

            //如果结果是 未记载
            //判読不能
            if (entryCommon.Equals("30") || entryCommon.Equals("20"))
            {
                return false;
            }


            IJaPrefixTelClientAccess jaPrefixTelClient = new JaPrefixTelClientAccess();

            foreach (var  rowResult in result)
            {
                foreach (var colResult in rowResult)
                {


                    //find tel part

                    var split = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat, colResult);

                    //得到的结果中有分隔符
                    string telResult = split.GetTel().Replace("/",string.Empty);
                    
                    if (EmptyResultCount(telResult).Equals(1))
                        continue;

                    bool containsTelA = true;
                    //如果录入结果包含市外番号，
                    //看看市外番号是否为空
                   if (ChildImageObject.CategorySetting.DataFormat.Contains("tel_a"))
                   {
                       string telA = split.GetTelA().Replace("/", string.Empty);
                       if (telA.Length == 0)
                       {
                           containsTelA = false;
                       }
                   }
                    //如果不包含 直接检测

                    //录入结果不为空
                    //没有问号
                    //市外局番号 不为空
                    if (EmptyResultCount(telResult) == 0 && 
                        QuestionMarkResultCount(telResult) == 0 &&
                        containsTelA )
                    {
                        bool find = false;

                        errorPrefixTel = telResult;
                        errTel = telResult;

                        foreach (int preLenth in PrefixTelLengthArray)
                        {
                            if (preLenth > 0 && telResult.Length >= preLenth)
                            {
                                string prefixTel = telResult.Substring(0, preLenth);

                                 find =  jaPrefixTelClient.FindPrefixTel(prefixTel, telResult.Length);

                                if (find)
                                {
                                    break;
                                }

                                errorPrefixTel = prefixTel;
                                errTel = telResult;
                            } 
                            
                        }

                        if (!find)
                        {
                            notfind = true;
                        }

                    }
                    else
                    {
                        notfind = true;
                    }


                    if (notfind)
                        break;
                }

                if (notfind)
                    break;
            }

            if (notfind)
            {
                System.Console.WriteLine("VerfityT0101");
                System.Console.Write(string.Format("tel:{0} prefixTel:{1}",errTel,errorPrefixTel));
            }

            return notfind;   
        }
    }
}